package com.edu.base.ums.student.service.impl;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.edu.base.ums.member.domain.ClsMember;
import com.edu.base.ums.student.domain.ClsMemberStudent;
import com.edu.base.ums.student.mapper.ClsMemberStudentMapper;
import com.edu.base.ums.student.service.IClsMemberStudentService;
import com.ruoyi.common.utils.bean.BeanUtils;

/**
 * 学生档案Service业务层处理
 * 
 * @author lzs
 * @date 2021-05-24
 */
@Service
public class ClsMemberStudentServiceImpl implements IClsMemberStudentService 
{
    @Autowired
    private ClsMemberStudentMapper clsMemberStudentMapper;

    /**
     * 查询学生档案
     * 
     * @param id 学生档案ID
     * @return 学生档案
     */
    @Override
    public ClsMemberStudent selectClsMemberStudentById(String id)
    {
        return clsMemberStudentMapper.selectClsMemberStudentById(id);
    }

    /**
     * 查询学生档案列表
     * 
     * @param clsMemberStudent 学生档案
     * @return 学生档案
     */
    @Override
    public List<ClsMemberStudent> selectClsMemberStudentList(ClsMemberStudent clsMemberStudent)
    {
        return clsMemberStudentMapper.selectClsMemberStudentList(clsMemberStudent);
    }

    /**
     * 新增学生档案
     * 
     * @param clsMemberStudent 学生档案
     * @return 结果
     */
    @Override
    public int insertClsMemberStudent(ClsMemberStudent clsMemberStudent)
    {
    	clsMemberStudent = (ClsMemberStudent)BeanUtils.initBeanPublicProperties(clsMemberStudent,"add");
        return clsMemberStudentMapper.insertClsMemberStudent(clsMemberStudent);
    }

    /**
     * 修改学生档案 
     * @param clsMemberStudent 学生档案
     * @return 结果
     */
    @Override
    public int updateClsMemberStudent(ClsMemberStudent clsMemberStudent)
    {
    	clsMemberStudent = (ClsMemberStudent)BeanUtils.initBeanPublicProperties(clsMemberStudent,"update");
        return clsMemberStudentMapper.updateClsMemberStudent(clsMemberStudent);
    }

    /**
     * 批量删除学生档案
     * 
     * @param ids 需要删除的学生档案ID
     * @return 结果
     */
    @Override
    public int deleteClsMemberStudentByIds(String[] ids)
    {
        return clsMemberStudentMapper.deleteClsMemberStudentByIds(ids);
    }

    /**
     * 删除学生档案信息
     * 
     * @param id 学生档案ID
     * @return 结果
     */
    @Override
    public int deleteClsMemberStudentById(String id)
    {
        return clsMemberStudentMapper.deleteClsMemberStudentById(id);
    }

	@Override
	public int updateClsMemberToCourse(ClsMember clsMember) {
		ClsMemberStudent clsMemberStudent = clsMemberStudentMapper.selectClsMemberStudentByMemberId(clsMember.getId());
		String courseId = clsMember.getCourseId();
		int result = 0;
		if (clsMemberStudent != null) {
			// TODO 两种情况 课程为空， 课程存在
			clsMemberStudent = (ClsMemberStudent) BeanUtils.initBeanPublicProperties(clsMemberStudent, "update");
			if (StringUtils.isNotBlank(clsMemberStudent.getCourseId())) {
				// TODO 取消开通课程权限
				String[] db_courseId = clsMemberStudent.getCourseId().split(",");
				List<String> list = Arrays.asList(db_courseId);
				List<String> filterlist = list.stream().filter(item -> !courseId.equals(item)).collect(Collectors.toList());
				String courseId_new = "null";
				if(list.size()>filterlist.size()) { 
					if(CollectionUtils.isNotEmpty(filterlist)) { 
						courseId_new = StringUtils.join(filterlist, ",");
 					}
				}else {
					courseId_new = StringUtils.join(list, ",") + "," + courseId;
				}
				
				clsMemberStudent.setCourseId(courseId_new);
			} else {
				clsMemberStudent.setCourseId(courseId);
			}
			result = clsMemberStudentMapper.updateClsMemberStudent(clsMemberStudent);
		}
		return result;
	}

	@Override
	public ClsMemberStudent selectClsMemberStudentByMemberId(String id) {
		return clsMemberStudentMapper.selectClsMemberStudentByMemberId(id);

	}
}
